
import React, {Fragment} from "react";
import { Button, Form} from "antd";
import Store from "./Store";
import {createModel} from "../../../api/work/GenerateApi";
import FormSelect from "../../../components/form/FormSelect";
import {observer} from "mobx-react";

@observer
class HasTableNoModel extends React.Component{
    constructor(props) {
        super(props);
        this.state={}
    }

    componentDidMount() {

    }

    generateModel = () => {
        this.props.form.validateFields((err, formData) => {
            if (!err){
                formData['page'] = Store.pageRoute;
                formData["config"] = JSON.stringify([]);
                formData["type"] = Store.CUSTOM_TYPE;
                createModel(formData).then(response => {
                    Store.resultVisible = true;
                    Store.title = response.data.msg;
                    Store.processList = response.data.processList;
                    Store.status = response.data.isSuccess ?  "success" : "error";
                    Store.modelInfo = response.data.modelInfo ? response.data.modelInfo : [];
            });
            }
        });
    };

    handleOnFocus = () => {
        // 修复通过数据表生成模型时新增的数据表不能显示
        console.log("获取数据表");
        Store.fetchAllTables();
    };

    render() {
        console.log(Store.allTables)
        return (
            <Fragment>
                <Form >
                    <FormSelect onFocus={this.handleOnFocus} form={this.props.form} label={"数据表"} field={"table_name"} options={Store.allTables} allowClear={true} showSearch={true} placeholder={"请选择数据表"} initialValue={Store.modelInfo["tableName"]} rules={[{ required: true, message: '菜数据表名不能为空' }]} />
                </Form>
                <Button onClick={this.generateModel} icon={"play-circle"} type="danger">
                    生成模型
                </Button>
            </Fragment>
        );
    }
}

export default Form.create()(HasTableNoModel)
